<?php
/**
*	内部上传文件，php上传到php
*/ 
class upfileClassAction extends apiAction
{

	/**
	*	分片上传
	*/
	public function indexAction()
	{
		$result 	= $this->getpostdata();
		$biaoshi	= (int)$this->get('biaoshi');
		$zong		= (int)$this->get('zong','1');
		$ci			= (int)$this->get('ci','0'); //当前次数
		$filename	= $this->get('filename');
		$filesize	= $this->get('filesize');
		$fileext	= $this->get('fileext');
		$fileexs	= $this->get('fileexs');
		if(!$fileexs)$fileexs = $fileext;
		
		$tema 		= ''.UPDIR.'/logs/'.$biaoshi.'_'.$fileext.'_'.$filesize.'';
		$temp 		= ''.$tema.'_'.$ci.'';
		$this->rock->createtxt($temp, base64_decode($result));
		$ubarr['ci'] = $ci;
		$ubarr['result'] = 'wait';
		
		//已经是最后一个(合并)
		if($ci==$zong-1){
			$path 	= ''.UPDIR.'/'.date('Y-m').'/'.date('d_His').rand(10,99).'.'.$fileext.'';
			$fp 	= fopen($path,'ab');
			for($oi=0;$oi<$zong;$oi++){
				$ptae = ''.$tema.'_'.$oi.'';
				fwrite($fp,file_get_contents($ptae));
				unlink($ptae);
			}
			fclose($fp);
			
			$picw = $pich = 0;
			if(contain(',jpg,png,jpeg,gif,bmp,',','.$fileexs.',')){
				$imgfo   = getimagesize($path);
				$picw 	= $imgfo[0];
				$pich 	= $imgfo[1];
			}
			$upses['oldfilename']   = $this->rock->jm->base64decode($this->get('filename'));
			$upses['filesize']	 	= $filesize;
			$upses['filesizecn']	= $this->rock->formatsize($filesize);
			$upses['allfilename']	= $path;
			$upses['picw']	 		= $picw;
			$upses['pich']	 		= $pich;
			$upses['fileext']	 	= $fileexs;
			
			$barr = c('down')->uploadback($upses,'',true, array(
				'agentid' => $this->agentid
			));
			
			//成功保存
			if(isset($barr['id'])){
				$filenum = arrvalue($barr, 'filenum');
				$id 	 = arrvalue($barr, 'id');
				$ubarr['result']  = 'ok';
				$ubarr['fileid']  = $id;
				$ubarr['filenum'] = $filenum;
				if($picw>0){
					$thumb = arrvalue($barr, 'autoup_thumb');
					if(!$thumb)$thumb = '{FILEURL}'.$barr['thumbpath'];
					$ubarr['thumbpath'] = $thumb;
				}
			}
		}
		
		return returnsuccess($ubarr);
	}
	

}